package com.kaishengit.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaishengit.dao.UserDao;
import com.kaishengit.pojo.User;

@Service
@Transactional
public class UserService {

	@Resource
	private UserDao userDao;
	
	public User findById(Integer id){
		return userDao.findById(id);
	}

	public void save(User user) {
		userDao.save(user);
	}

	public void deleteById(int i) {
		userDao.delete(i);
	}

	public void deleteByObject(User user) {
		userDao.delete(user);
	}
	
	public User findByProperties(String username,String userpwd){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("userpwd", userpwd);
		return userDao.findByProperties(map);
	}

	public List<User> findAll() {
		return userDao.findAll();
	}
	
	public List<User> findByIds(String users){
		String[] ids = users.split(",");
		List<User> userList = new ArrayList<User>();
		for(String id : ids){
			userList.add(userDao.findById(Integer.valueOf(id)));
		}
		return userList;
	}
	
	public User findByLoginName(String username){
		return userDao.findByProperty("username", username);
	}
}
